package db

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
)

var mysqlDB *sql.DB
var mysqlConn string

func InitMysql(conn string) {
	mysqlConn = conn
}

func newMysqlPool(conn string) (*sql.DB, error) {
	db, err := sql.Open("mysql", conn)
	if err != nil {
		return nil, err
	}

	// Open doesn't open a connection. Validate DSN data:
	err = db.Ping()
	if err != nil {
		return nil, err
	}
	return db, nil
}

func GetMysqlPool() (*sql.DB, error) {
	if mysqlDB == nil {
		return newMysqlPool(mysqlConn)
	}
	return mysqlDB, nil
}
